<template>  
  <div class="system-cameraStatusLog-edit">
    <!-- 添加或修改摄像头状态日志对话框 -->
    <el-dialog v-model="isShowDialog" width="769px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.system-cameraStatusLog-edit .el-dialog', '.system-cameraStatusLog-edit .el-dialog__header']">{{(!formData.id || formData.id==0?'添加':'修改')+'摄像头状态日志'}}</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="90px">       
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { reactive, toRefs, defineComponent,ref,unref,getCurrentInstance } from 'vue';
import {ElMessageBox, ElMessage, FormInstance,UploadProps} from 'element-plus';
import {
  listCameraStatusLog,
  getCameraStatusLog,
  delCameraStatusLog,
  addCameraStatusLog,
  updateCameraStatusLog,  
  listCameras,  
} from "/@/api/system/cameraStatusLog";
import {
  CameraStatusLogTableColumns,
  CameraStatusLogInfoData,
  CameraStatusLogTableDataState,
  CameraStatusLogEditState,  
  LinkedCameraStatusLogCameras,  
} from "/@/views/system/cameraStatusLog/list/component/model"
export default defineComponent({
  name:"apiV1SystemCameraStatusLogEdit",
  components:{    
  },
  props:{    
    gbIdOptions:{
      type:Array,
      default:()=>[]
    },    
    statusOptions:{
      type:Array,
      default:()=>[]
    },    
  },
  setup(props,{emit}) {    
    const {proxy} = <any>getCurrentInstance()
    const formRef = ref<HTMLElement | null>(null);
    const menuRef = ref();    
    const state = reactive<CameraStatusLogEditState>({
      loading:false,
      isShowDialog: false,
      formData: {        
        id: undefined,        
        gbId: undefined,        
        status: false ,        
        createdAt: undefined,        
        linkedCameraStatusLogCameras: {          
          gbId:undefined,    // 国标号          
          name:undefined,    // 名称          
        },        
      },
      // 表单校验
      rules: {        
        id : [
            { required: true, message: "序号不能为空", trigger: "blur" }
        ],        
        gbId : [
            { required: true, message: "国标id不能为空", trigger: "blur" }
        ],        
        status : [
            { required: true, message: "状态不能为空", trigger: "blur" }
        ],        
      }
    });
    // 打开弹窗
    const openDialog = (row?: CameraStatusLogInfoData) => {
      resetForm();
      if(row) {        
        getCameraStatusLog(row.id!).then((res:any)=>{
          const data = res.data;          
          state.formData = data;
      })
    }
      state.isShowDialog = true;
    };
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef) as any;
      if (!formWrap) return;
      formWrap.validate((valid: boolean) => {
        if (valid) {
          state.loading = true;
          if(!state.formData.id || state.formData.id===0){
            //添加
          addCameraStatusLog(state.formData).then(()=>{
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('cameraStatusLogList')
            }).finally(()=>{
              state.loading = false;
            })
          }else{
            //修改
          updateCameraStatusLog(state.formData).then(()=>{
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('cameraStatusLogList')
            }).finally(()=>{
              state.loading = false;
            })
          }
        }
      });
    };
    const resetForm = ()=>{
      state.formData = {        
        id: undefined,        
        gbId: undefined,        
        status: false ,        
        createdAt: undefined,        
        linkedCameraStatusLogCameras: {          
          gbId:undefined,    // 国标号          
          name:undefined,    // 名称          
        },        
      }
    };    
    //关联cameras表选项
    const getCamerasItemsGbId = () => {
      emit("getCamerasItemsGbId")
    }    
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,      
      getCamerasItemsGbId,      
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>  
</style>